# Current, as of Thursday, May 28, 2020.
# rm(list = ls())
# setwd("~/Dropbox/Base Dropbox/List Experiment Paper (PS)/Conditional Accept/Replication Files (Carmines-Schmidt PS)/2012 CCAP/ANOVA Tests")
library(foreign)
library(car)
library(readstata13)
library(zeligverse)
library(ggplot2)
# install.packages("stargazer", dependencies = TRUE)
library(stargazer)
library(apsrtable)
library(plyr)
#-----
# Input the data.
our.data = read.dta13("CarminesSchmidtPS-replication-2012.dta", generate.factors = TRUE)
names(ccap.2012)
#-----
# CCAP 2012, preparing data for ANOVA tests. Respondents were assigned to either the "African American candidate," 
# "Control," "Gay or Homosexual candidate," "Muslim candidate," or "Mormon candidate" LEs. 

# Indicator variable: assignment to ANY of the sensitive-item conditions. 
ccap.2012$sensitive.item = ifelse(ccap.2012$treatment!="Control", "Sensitive-Item", "Control")
table(ccap.2012$sensitive.item)
table(ccap.2012$sensitive.item, ccap.2012$treatment)
# Dichotomous, numeric version of this variable (1 = sensitive-item conditions; 0 = treatment condition).
ccap.2012$sensitive.item.binary = ifelse(ccap.2012$sensitive.item=="Sensitive-Item", 1, 0) 
table(ccap.2012$sensitive.item.binary)
table(ccap.2012$sensitive.item.binary, ccap.2012$treatment)
# Factor variable.
ccap.2012$sensitive.item.factor = as.factor(ccap.2012$sensitive.item)
table(ccap.2012$sensitive.item.factor)
#-----
# Divide the dataset into four subsamples: each with the baseline condition and ONE of the sensitive-item conditions.
# The 'African American' and baseline LE subsets should be just non-African-American respondents. 

# 'African American' and baseline
afroam.experiment = subset(ccap.2012, treatment=="African Americans" | treatment=="Control")
afroam.experiment = subset(afroam.experiment, black!=1)
table(afroam.experiment$treatment) 

# 'Gay or Homosexual' and baseline
gays.experiment = subset(ccap.2012, treatment=="Gays" | treatment=="Control")
table(gays.experiment$treatment)

# 'Muslim' and baseline
muslims.experiment = subset(ccap.2012, treatment=="Muslims" | treatment=="Control")
table(muslims.experiment$treatment)

# 'Mormon' and baseline 
mormons.experiment = subset(ccap.2012, treatment=="Mormons" | treatment=="Control")
table(mormons.experiment$treatment)
#-----
# To run the ANOVA tests, make assignment-to-treatment a factor variable, for each of the four datasets.

# Treatment (factor), "African American" and baseline
afroam.experiment$treatment.factor = as.factor(afroam.experiment$treatment)
table(afroam.experiment$treatment.factor)
levels(afroam.experiment$treatment.factor)

# Treatment (factor), "Gay or Homosexual" and baseline
gays.experiment$treatment.factor = as.factor(gays.experiment$treatment)
table(gays.experiment$treatment.factor)
levels(gays.experiment$treatment.factor)

# Treatment (factor), "Muslim" and baseline
muslims.experiment$treatment.factor = as.factor(muslims.experiment$treatment)
table(muslims.experiment$treatment.factor)
levels(muslims.experiment$treatment.factor)

# Treatment (factor), "Mormon" and baseline
mormons.experiment$treatment.factor = as.factor(mormons.experiment$treatment)
table(mormons.experiment$treatment.factor)
levels(mormons.experiment$treatment.factor)
#-----
# ANOVA tests for balance: AFRICAN AMERICAN and BASELINE conditions (excluding African American respondents)

# 'African American' and baseline LEs
# Partisanship 
pid.fit = aov(pid_7 ~ treatment.factor, data = afroam.experiment)
summary(pid.fit)
# Ideology
ideology.fit = aov(ideology ~ treatment.factor, data = afroam.experiment)
summary(ideology.fit)
# Gender
gender.fit = aov(female ~ treatment.factor, data = afroam.experiment)
summary(gender.fit)
# White
white.fit = aov(white ~ treatment.factor, data = afroam.experiment)
summary(white.fit)
# Education
education.fit = aov(education ~ treatment.factor, data = afroam.experiment)
summary(education.fit)
# Church attendance
church.fit = aov(church ~ treatment.factor, data = afroam.experiment)
summary(church.fit)
# Racial resentment
rr.fit = aov(rr ~ treatment.factor, data = afroam.experiment)
summary(rr.fit)
# Obama favorability
obama.fit = aov(obama_favor ~ treatment.factor, data = afroam.experiment)
summary(obama.fit)
# Political interest
interest.fit = aov(interest ~ treatment.factor, data = afroam.experiment)
summary(interest.fit)
# Family income 
income.fit = aov(income ~ treatment.factor, data = afroam.experiment)
summary(income.fit)

# Use the 'plyr' package to take the means of these different variables across the conditions.
# This simply helps concatenate the output into a meaningful table.
our.means.afroam = ddply(afroam.experiment, .(treatment.factor), summarize, 
                         mean.pid = round(mean(pid_7, na.rm=TRUE), digits = 2),
                         mean.ideology = round(mean(ideology, na.rm=TRUE), digits = 2),
                         mean.female = round(mean(female, na.rm=TRUE), digits = 2),
                         mean.white = round(mean(white, na.rm=TRUE), digits = 2),
                         mean.education = round(mean(education, na.rm=TRUE), digits = 2),
                         mean.church = round(mean(church, na.rm=TRUE), digits = 2),
                         mean.rr = round(mean(rr, na.rm=TRUE), digits = 2),
                         mean.obama = round(mean(obama_favor, na.rm=TRUE), digits = 2),
                         mean.interest = round(mean(interest, na.rm=TRUE), digits = 2),
                         mean.income = round(mean(income, na.rm=TRUE), digits = 2))
our.means.afroam
#-----
# ANOVA tests for balance: GAY-OR-HOMOSEXUAL and BASELINE conditions

# 'Gay or Homosexual' and baseline LEs
# Partisanship 
pid.fit = aov(pid_7 ~ treatment.factor, data = gays.experiment)
summary(pid.fit)
# Ideology
ideology.fit = aov(ideology ~ treatment.factor, data = gays.experiment)
summary(ideology.fit)
# Gender
gender.fit = aov(female ~ treatment.factor, data = gays.experiment)
summary(gender.fit)
# Black
black.fit = aov(black ~ treatment.factor, data = gays.experiment)
summary(black.fit)
# White
white.fit = aov(white ~ treatment.factor, data = gays.experiment)
summary(white.fit)
# Education
education.fit = aov(education ~ treatment.factor, data = gays.experiment)
summary(education.fit)
# Church attendance
church.fit = aov(church ~ treatment.factor, data = gays.experiment)
summary(church.fit)
# Racial resentment
rr.fit = aov(rr ~ treatment.factor, data = gays.experiment)
summary(rr.fit)
# Obama favorability
obama.fit = aov(obama_favor ~ treatment.factor, data = gays.experiment)
summary(obama.fit)
# Political interest
interest.fit = aov(interest ~ treatment.factor, data = gays.experiment)
summary(interest.fit)
# Family income 
income.fit = aov(income ~ treatment.factor, data = gays.experiment)
summary(income.fit)

# Use the 'plyr' package to take the means of these different variables across the conditions.
# This simply helps concatenate the output into a meaningful table.
our.means.gays = ddply(gays.experiment, .(treatment.factor), summarize, 
                       mean.pid = round(mean(pid_7, na.rm=TRUE), digits = 2),
                       mean.ideology = round(mean(ideology, na.rm=TRUE), digits = 2),
                       mean.female = round(mean(female, na.rm=TRUE), digits = 2),
                       mean.white = round(mean(white, na.rm=TRUE), digits = 2),
                       mean.black = round(mean(black, na.rm=TRUE), digits = 2),
                       mean.education = round(mean(education, na.rm=TRUE), digits = 2),
                       mean.church = round(mean(church, na.rm=TRUE), digits = 2),
                       mean.rr = round(mean(rr, na.rm=TRUE), digits = 2),
                       mean.obama = round(mean(obama_favor, na.rm=TRUE), digits = 2),
                       mean.interest = round(mean(interest, na.rm=TRUE), digits = 2),
                       mean.income = round(mean(income, na.rm=TRUE), digits = 2))
our.means.gays
#-----
# ANOVA tests for balance: MUSLIM and BASELINE conditions

# 'Muslim' and baseline LEs
# Partisanship 
pid.fit = aov(pid_7 ~ treatment.factor, data = muslims.experiment)
summary(pid.fit)
# Ideology
ideology.fit = aov(ideology ~ treatment.factor, data = muslims.experiment)
summary(ideology.fit)
# Gender
gender.fit = aov(female ~ treatment.factor, data = muslims.experiment)
summary(gender.fit)
# Black
black.fit = aov(black ~ treatment.factor, data = muslims.experiment)
summary(black.fit)
# White
white.fit = aov(white ~ treatment.factor, data = muslims.experiment)
summary(white.fit)
# Education
education.fit = aov(education ~ treatment.factor, data = muslims.experiment)
summary(education.fit)
# Church attendance
church.fit = aov(church ~ treatment.factor, data = muslims.experiment)
summary(church.fit)
# Racial resentment
rr.fit = aov(rr ~ treatment.factor, data = muslims.experiment)
summary(rr.fit)
# Obama favorability
obama.fit = aov(obama_favor ~ treatment.factor, data = muslims.experiment)
summary(obama.fit)
# Political interest
interest.fit = aov(interest ~ treatment.factor, data = muslims.experiment)
summary(interest.fit)
# Family income 
income.fit = aov(income ~ treatment.factor, data = muslims.experiment)
summary(income.fit)

# Use the 'plyr' package to take the means of these different variables across the conditions.
# This simply helps concatenate the output into a meaningful table.
our.means.muslims = ddply(muslims.experiment, .(treatment.factor), summarize, 
                          mean.pid = round(mean(pid_7, na.rm=TRUE), digits = 2),
                          mean.ideology = round(mean(ideology, na.rm=TRUE), digits = 2),
                          mean.female = round(mean(female, na.rm=TRUE), digits = 2),
                          mean.white = round(mean(white, na.rm=TRUE), digits = 2),
                          mean.black = round(mean(black, na.rm=TRUE), digits = 2),
                          mean.education = round(mean(education, na.rm=TRUE), digits = 2),
                          mean.church = round(mean(church, na.rm=TRUE), digits = 2),
                          mean.rr = round(mean(rr, na.rm=TRUE), digits = 2),
                          mean.obama = round(mean(obama_favor, na.rm=TRUE), digits = 2),
                          mean.interest = round(mean(interest, na.rm=TRUE), digits = 2),
                          mean.income = round(mean(income, na.rm=TRUE), digits = 2))
our.means.muslims
#-----
# ANOVA tests for balance: MORMON and BASELINE conditions

# 'Mormon' and baseline LEs
# Partisanship 
pid.fit = aov(pid_7 ~ treatment.factor, data = mormons.experiment)
summary(pid.fit)
# Ideology
ideology.fit = aov(ideology ~ treatment.factor, data = mormons.experiment)
summary(ideology.fit)
# Gender
gender.fit = aov(female ~ treatment.factor, data = mormons.experiment)
summary(gender.fit)
# Black
black.fit = aov(black ~ treatment.factor, data = mormons.experiment)
summary(black.fit)
# White
white.fit = aov(white ~ treatment.factor, data = mormons.experiment)
summary(white.fit)
# Education
education.fit = aov(education ~ treatment.factor, data = mormons.experiment)
summary(education.fit)
# Church attendance
church.fit = aov(church ~ treatment.factor, data = mormons.experiment)
summary(church.fit)
# Racial resentment
rr.fit = aov(rr ~ treatment.factor, data = mormons.experiment)
summary(rr.fit)
# Obama favorability
obama.fit = aov(obama_favor ~ treatment.factor, data = mormons.experiment)
summary(obama.fit)
# Political interest
interest.fit = aov(interest ~ treatment.factor, data = mormons.experiment)
summary(interest.fit)
# Family income 
income.fit = aov(income ~ treatment.factor, data = mormons.experiment)
summary(income.fit)
# Romney favorability
romney.fit = aov(romney_favor ~ treatment.factor, data = mormons.experiment)
summary(romney.fit)

# Use the 'plyr' package to take the means of these different variables across the conditions.
# This simply helps concatenate the output into a meaningful table.
our.means.mormons = ddply(mormons.experiment, .(treatment.factor), summarize, 
                          mean.pid = round(mean(pid_7, na.rm=TRUE), digits = 2),
                          mean.ideology = round(mean(ideology, na.rm=TRUE), digits = 2),
                          mean.female = round(mean(female, na.rm=TRUE), digits = 2),
                          mean.white = round(mean(white, na.rm=TRUE), digits = 2),
                          mean.black = round(mean(black, na.rm=TRUE), digits = 2),
                          mean.education = round(mean(education, na.rm=TRUE), digits = 2),
                          mean.church = round(mean(church, na.rm=TRUE), digits = 2),
                          mean.rr = round(mean(rr, na.rm=TRUE), digits = 2),
                          mean.obama = round(mean(obama_favor, na.rm=TRUE), digits = 2),
                          mean.interest = round(mean(interest, na.rm=TRUE), digits = 2),
                          mean.income = round(mean(income, na.rm=TRUE), digits = 2),
                          mean.romney = round(mean(romney_favor, na.rm = TRUE), digits = 2))
our.means.mormons
#-----
# ANOVA tests for balance: SENSITIVE-ITEM CONDITIONS (pooled) and BASELINE conditions

# Pooled sensitive-item conditions and baseline LEs
# Partisanship 
pid.fit = aov(pid_7 ~ sensitive.item.factor, data = ccap.2012)
summary(pid.fit)
# Ideology
ideology.fit = aov(ideology ~ sensitive.item.factor, data = ccap.2012)
summary(ideology.fit)
# Gender
gender.fit = aov(female ~ sensitive.item.factor, data = ccap.2012)
summary(gender.fit)
# Black
black.fit = aov(black ~ sensitive.item.factor, data = ccap.2012)
summary(black.fit)
# White
white.fit = aov(white ~ sensitive.item.factor, data = ccap.2012)
summary(white.fit)
# Education
education.fit = aov(education ~ sensitive.item.factor, data = ccap.2012)
summary(education.fit)
# Church attendance
church.fit = aov(church ~ sensitive.item.factor, data = ccap.2012)
summary(church.fit)
# Racial resentment
rr.fit = aov(rr ~ sensitive.item.factor, data = ccap.2012)
summary(rr.fit)
# Obama favorability
obama.fit = aov(obama_favor ~ sensitive.item.factor, data = ccap.2012)
summary(obama.fit)
# Political interest
interest.fit = aov(interest ~ sensitive.item.factor, data = ccap.2012)
summary(interest.fit)
# Family income 
income.fit = aov(income ~ sensitive.item.factor, data = ccap.2012)
summary(income.fit)
# Romney favorability 
romney.fit = aov(romney_favor ~ sensitive.item.factor, data = ccap.2012)
summary(romney.fit)

# Use the 'plyr' package to take the means of these different variables across the conditions.
# This simply helps concatenate the output into a meaningful table.
our.means = ddply(ccap.2012, .(sensitive.item.factor), summarize, 
                  mean.pid = round(mean(pid_7, na.rm=TRUE), digits = 2),
                  mean.ideology = round(mean(ideology, na.rm=TRUE), digits = 2),
                  mean.female = round(mean(female, na.rm=TRUE), digits = 2),
                  mean.white = round(mean(white, na.rm=TRUE), digits = 2),
                  mean.black = round(mean(black, na.rm=TRUE), digits = 2),
                  mean.education = round(mean(education, na.rm=TRUE), digits = 2),
                  mean.church = round(mean(church, na.rm=TRUE), digits = 2),
                  mean.rr = round(mean(rr, na.rm=TRUE), digits = 2),
                  mean.obama = round(mean(obama_favor, na.rm=TRUE), digits = 2),
                  mean.interest = round(mean(interest, na.rm=TRUE), digits = 2),
                  mean.income = round(mean(income, na.rm=TRUE), digits = 2),
                  mean.romney = round(mean(romney_favor, na.rm=TRUE), digits = 2))
our.means

